package com.hqyj.paimaisys.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqyj.paimaisys.model.Huiyuan;
import com.hqyj.paimaisys.service.HuiyuanService;
import com.hqyj.paimaisys.util.MailUtil;
import com.hqyj.paimaisys.util.TokenUtil;
import com.hqyj.paimaisys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/huiyuan")
public class HuiyuanController {

    @Autowired
    private HuiyuanService huiyuanService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${accessTokenExpireTime}")
    private Integer accessTokenExpireTime;

    @Autowired
    private MailUtil mailUtil;


    @RequestMapping("/register")
    public ResponseResult<String> register(@RequestBody Huiyuan hy) throws MessagingException {
        //1. 完善会员的三个属性 余额， 冻结金额， 状态码
        hy.setHyue(0);
        hy.setHicemoney(0);
        hy.setRid(Huiyuan.STATUS_INACTIVE);

        //2. 新增到会员表中
        boolean save = huiyuanService.save(hy);
        if(save){
            //发激活邮件
            mailUtil.sendJihuoMail(hy.getHemail(), hy.getHid());
            return ResponseResult.success("注册成功");
        }else{
            return ResponseResult.error("注册失败");
        }

    }

    @RequestMapping("/login")
    public ResponseResult<String> login(String hid, String pwd) throws Exception {
        //1. 调用service，根据hid主键进行查询
        Huiyuan hy = huiyuanService.getById(hid);
        //2.1 查询不到， 账号错误
        if(hy == null){
            return ResponseResult.error("账号输入错误");
        }
        //2.2 查询到了， 密码错误
        if(!pwd.equals(hy.getHpwd())){
            return ResponseResult.error("密码输入错误");
        }
        //2.3 查询到了， 用户名和密码都对， 状态为未激活
        if(hy.getRid() == Huiyuan.STATUS_INACTIVE){
            return ResponseResult.error("账户未激活");
        }
        //2.4 全部正确，登录成功
        //3. 为hid进行加密，生成token, 发送到页面
        String token = TokenUtil.sign(hid);

        //key:  token     value : hid, 缓存到redis中。
        redisTemplate.opsForValue().set(token, hid, accessTokenExpireTime, TimeUnit.SECONDS);

        return ResponseResult.success(token);

    }

    @RequestMapping("/validHid")
    public ResponseResult<String> register(String hid){
       //1.根据会员编号 hid 查询会员对象
        Huiyuan hy = huiyuanService.getById(hid);
       //2.1 如果没查询到，那么hid没重复， 可用
        if(hy == null){
            return ResponseResult.success();
        }

       //2.2 查询到了， 不可用的

          return   ResponseResult.error();

    }

    @RequestMapping("active")
    //处理账户激活请求
    public String active(String hid){
        UpdateWrapper<Huiyuan> updateWrapper = new UpdateWrapper<Huiyuan>();
        updateWrapper.set("rid", Huiyuan.STATUS_ACTIVE);
        updateWrapper.eq("hid", hid);
        huiyuanService.update(updateWrapper);
        return "激活成功";
    }

}

